<?php

/**
 * BlockController
 * 
 * @author Radovan Dvorsky
 * @version 20100514
 */

require_once 'Zend/Controller/Action.php';

class BlockController extends Zend_Controller_Action {
	
	public function init(){
		$contextSwitch = $this->_helper->getHelper('AjaxContext');
        $contextSwitch->addActionContext('show-block', 'html')
        ->addActionContext('add-subject', 'html')
        ->addActionContext('remove-subject', 'html')
        ->addActionContext('remove-program', 'html')
        ->addActionContext('add-program', 'html')       
        ->initContext();
	}
	
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		
		$cache = Zend_Registry::get('cache');
		$cacheName = My_Support::getCacheName($this,'fetchAll');
		
		
		if(!($blocks = $cache->load($cacheName))){
			$blockMapper = new Db_Mapper_Block();
			$blocks = $blockMapper->fetchAll();
			
			$cache->save($blocks,$cacheName,array(get_class($blockMapper)));
		}
		
		$this->view->blocks = $blocks;
	}
	
	
	public function addAction(){
		
		$form = new Form_Block_Add();
		
		$this->view->form = $form;
		
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				try{	

					$subjectArrayList = array();
					
					foreach ($formData['subjects'] as $subject){
						$subjectArrayList[] = new Db_Model_Subject(array('sid' => $subject));
					}
					
					$blockMapper = new Db_Mapper_Block();
					$blockModel = new Db_Model_Block(array('name' => $formData['name']));
					$blockModel->setSubjects($subjectArrayList);
					
					$blockMapper->save($blockModel,true);					
					
					$cache = Zend_Registry::get('cache');
					$cache->remove(My_Support::getCacheName($this,'fetchAll'));
					
					$this->view->message = '<p class="success">Odobrný blok pridaný</p>';
					
				} catch (Zend_Exception $e){
					
					$log = Zend_Registry::get('log');    		
		    		$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
					
					$this->view->message = '<p class="error">Odobrný blok sa nepodarilo pridať</p>';
				}
			}
		}
	}
	
	public function showBlockAction(){
				
		$url = $this->view->url(array(
			'controller' => 'block',
			'action' => 'add-subject',
			'format' => 'html'
			)
		);
		
		$urlAddProgram = $this->view->url(array(
			'controller' => 'block',
			'action' => 'add-program',
			'format' => 'html'
			)
		);		
		
		$addProgramForm = new Form_Block_AddProgram($urlAddProgram,'table.groupsTable');
		
		$this->view->addProgramForm = $addProgramForm;
		
		$bid = (int)$this->getRequest()->getParam('bid');
		$form = new Form_Block_AddSubject($url,'.subjectsTable');
		$form->getElement('bid')->setValue($bid);
		$this->view->form = $form;
		
		$cache = Zend_Registry::get('cache');
		$cacheName = My_Support::getCacheName($this,'show-block'.$bid);
		$programmesCacheName = My_Support::getCacheName($this,'show-group'.$bid);
							
		if(!($block = $cache->load($cacheName))){
			$blockMapper = new Db_Mapper_Block();
			$order = $blockMapper->getTable()->select()->order('name');
			$blockModel = new Db_Model_Block(array('bid' => $bid));			
			$block = $blockMapper->fetchWithDependentRowset('Db_DbTable_Subject','Db_DbTable_SubjectBlock',$blockModel,$order);			
			$cache->save($block,$cacheName,array(get_class($this)));			
		}	
		
		if(!($programmes = $cache->load($programmesCacheName))){			
			$blockMapper = new Db_Mapper_Block();
			$blockModel = new Db_Model_Block(array('bid' => $bid));
			$programmes = $blockMapper->fetchWithDependentRowset('Db_DbTable_StObory','Db_DbTable_BlockStObory',$blockModel);			
			$cache->save($programmes,$programmesCacheName,array(get_class($this)));			
		}
		
		$this->view->programmes = $programmes;
		$this->view->block = $block;		
	}
	
	public function addSubjectAction(){
		
		$bid = (int)$this->getRequest()->getParam('bid');
		$sid = (int)$this->getRequest()->getParam('sid');		
		
		
		try{
			$subjectMapper = new Db_Mapper_Subject();
			$this->view->subjectModel = $subjectMapper->find(new Db_Model_Subject(array('sid' => $sid)));			
			
			$this->view->subjectBlockModel = new Db_Model_SubjectBlock(array('sid' => $sid, 'bid' => $bid));			
			$subjectBlockMapper = new Db_Mapper_SubjectBlock();
			$this->view->insert = $subjectBlockMapper->save($this->view->subjectBlockModel,true);
			
			$cache = Zend_Registry::get('cache');
			$cache->remove(My_Support::getCacheName($this,'show-block'.$bid));
			
		} catch (Zend_Exception $e){
			
			$log = Zend_Registry::get('log');    		
		    $log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
					
			$this->view->message = '<p class="error">Premdet sa nepodarilo pridať</p>';
		}
		
	}
	
	public function addProgramAction(){				
		$Id_oboru = (int)$this->getRequest()->getParam('Id_oboru');
		$bid = (int)$this->getRequest()->getParam('bid');
				
		try{			
			$this->view->Id_oboru = $Id_oboru;
			$this->view->bid = $bid;
			
			$blockStOboryMapper = new Db_Mapper_BlockStObory();			
			$model = new Db_Model_BlockStObory(array('Id_oboru' => $Id_oboru,'bid' => $bid));
			$id = $blockStOboryMapper->save($model,true);
			
			/*
			 * Ak sa nieco vlozilo do DB
			 */
			if($id > 0){
				$stOboryMapper = new Db_Mapper_StObory();
				$this->view->program = $stOboryMapper->find(new Db_Model_StObory(array('Id_oboru' => $Id_oboru)));
			}
			
			$cache = Zend_Registry::get('cache');
			$cache->remove(My_Support::getCacheName($this,'show-group'.$bid));
			
			$skRokMapper = new Db_Mapper_SkRok();
			$TTridyMapper = new Db_Mapper_TTridy();
			$selectGroups = $TTridyMapper->getTable()->select();
			$selectGroups->where('id_oboru = ?',$Id_oboru,Zend_Db::INT_TYPE)
			->where('vznik = ?',$skRokMapper->getActual()->getID_sk(),Zend_Db::INT_TYPE)
			->where('Zkratka LIKE \'3%\'');			
			$groups = $TTridyMapper->fetchAll($selectGroups);
			
			/*
			 * Vymaze cache pre vsetky triedy pre dany obor
			 */
			foreach($groups as $group){			
				$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array('Result_ShowUser_ID_tr'.$group->ID_tr));
			}
		} catch (Zend_Exception $e){			
			$log = Zend_Registry::get('log');    		
		    $log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);			
		}
	}
	
	public function removeSubjectAction(){
		
		$bid = (int)$this->getRequest()->getParam('bid');
		$sid = (int)$this->getRequest()->getParam('sid');

		try{
			
			$subjectBlockMapper = new Db_Mapper_SubjectBlock();
			
			$where = $subjectBlockMapper->getTable()->getAdapter()->quoteInto('bid = ? ',$bid,Zend_Db::INT_TYPE) . 
					 $subjectBlockMapper->getTable()->getAdapter()->quoteInto('And sid = ?',$sid,Zend_Db::INT_TYPE);
					 
			$subjectBlockMapper->getTable()->delete($where);
			
			$cache = Zend_Registry::get('cache');
			$cache->remove(My_Support::getCacheName($this,'show-block'.$bid));
			
		
		} catch (Zend_Exception $e){
			
			$log = Zend_Registry::get('log');    		
		    $log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
					
			$this->view->message = '<p class="error">Premdet sa nepodarilo odstrániť</p>';
		}
	}
	
	public function removeProgramAction(){
		
		$Id_oboru = (int)$this->getRequest()->getParam('Id_oboru');
		$bid = (int)$this->getRequest()->getParam('bid');
		
		try{

			$blockStOboryMapper = new Db_Mapper_BlockStObory();
			
			$adapter = $blockStOboryMapper->getTable()->getAdapter();
						
			$where = $adapter->quoteInto('bid = ? AND ',$bid,Zend_Db::INT_TYPE)
				   . $adapter->quoteInto('Id_oboru = ?',$Id_oboru,Zend_Db::INT_TYPE); 
					 
			$blockStOboryMapper->getTable()->delete($where);

			$cache = Zend_Registry::get('cache');
			$cache->remove(My_Support::getCacheName($this,'show-group'.$bid));
			
			$skRokMapper = new Db_Mapper_SkRok();
			$TTridyMapper = new Db_Mapper_TTridy();
			$selectGroups = $TTridyMapper->getTable()->select();
			$selectGroups->where('id_oboru = ?',$Id_oboru,Zend_Db::INT_TYPE)
			->where('vznik = ?',$skRokMapper->getActual()->getID_sk(),Zend_Db::INT_TYPE)
			->where('Zkratka LIKE \'3%\'');			
			$groups = $TTridyMapper->fetchAll($selectGroups);
			
			/*
			 * Vymaze cache pre vsetky triedy pre dany obor
			 */
			foreach($groups as $group){			
				$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array('Result_ShowUser_ID_tr'.$group->ID_tr));
			}	
			
		}catch (Zend_Exception $e){
			$log = Zend_Registry::get('log');    		
		    $log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
					
			$this->view->message = '<p class="error">Skupinu sa nepodarilo odstrániť</p>';
		}		
	}

}